package controllers

import (
	"net/http"
	"time"
)

type (
	Code struct {
		Code     int    `json:"code"`
		Message  string `json:"message"`
		HttpCode int    `json:"-"`
	}

	Response struct {
		*Code
		Result interface{} `json:"data,omitempty"`
	}
)

func SuccCodeCallId(callId string) *Code {
	return &Code{
		Code:     0,
		Message:  callId,
		HttpCode: http.StatusOK,
	}
}

func makeCode(httpCode, code int, msg string) *Code {
	return &Code{
		Code:     code,
		Message:  msg,
		HttpCode: httpCode,
	}
}

func MakeSysErrCode(msg string) *Code {
	return &Code{
		Code:     500009,
		Message:  msg,
		HttpCode: 500,
	}
}

func MakeSysErrWithCode(code int, msg string) *Code {
	return &Code{
		Code:     code,
		Message:  msg,
		HttpCode: 500,
	}
}

func (c *Code) SetMsg(msg string) *Code {
	code := new(Code)
	code.Code = c.Code
	code.HttpCode = c.HttpCode
	code.Message = msg

	return code
}

func MakeResp(code *Code, data interface{}) *Response {
	return &Response{
		Code:   code,
		Result: data,
	}
}

// 状态码定义
// 成功状态码为0
// 错误状态码为6位int
// 前三位为错误对应的http status
// 后三位为自增的错误码
var (
	Success = makeCode(http.StatusOK, 0, "success")
	Created = makeCode(http.StatusCreated, 0, "created")

	ParamsEmpty         = makeCode(http.StatusBadRequest, 400001, "必填项不能为空")
	ParamsError         = makeCode(http.StatusBadRequest, 400002, "参数不合法")
	ServiceIdError      = makeCode(http.StatusBadRequest, 400003, "serviceId必须为数字")
	SipIdError          = makeCode(http.StatusBadRequest, 400004, "account必须为数字")
	NumberError         = makeCode(http.StatusBadRequest, 400005, "号码不正确")
	SipIdNotExist       = makeCode(http.StatusBadRequest, 400006, "sip账户不存在")
	ServiceIdTooShort   = makeCode(http.StatusBadRequest, 400007, "坐席Id需要大于4位")
	AppIdNotExist       = makeCode(http.StatusNotFound, 404000, "appId不存在")
	QueRegFail          = makeCode(http.StatusNotFound, 404001, "企业Id不存在")
	SignFail            = makeCode(http.StatusNotFound, 404002, "技能组不存在")
	NumberExist         = makeCode(http.StatusNotFound, 404003, "号码已被绑定")
	ResourceNotExist    = makeCode(http.StatusNotFound, 404004, "资源不存在或已被删除")
	FileNotExist        = makeCode(http.StatusNotFound, 404005, "放音文件不存在")
	IvrIdErr            = makeCode(http.StatusNotFound, 404006, "ivrId错误")
	FileIsTooLarge      = makeCode(http.StatusRequestEntityTooLarge, 413000, "上传文件不能超过100MB")
	RepeatedOperate     = makeCode(http.StatusBadRequest, 429001, "操作过于频繁, 请稍后再试")
	InternalError       = makeCode(http.StatusInternalServerError, 500001, "未知错误, 请稍后再试")
	CallIsFullError     = makeCode(http.StatusInternalServerError, 500002, "呼叫队列已满，请稍后重试")
	EventIsInvlid       = makeCode(http.StatusInternalServerError, 600001, "呼叫队列已满，请稍后重试")
	ServiceNeedRegister = makeCode(http.StatusInternalServerError, 600002, "坐席Sip话机未注册")
	ServiceNeedSignIn   = makeCode(http.StatusInternalServerError, 600003, "坐席需要先签入")
)

//企业注册入参
type EnterpriseRegParam struct {
	Number   string `json:"number"`   //接听号码/外显号码
	AppId    string `json:"appId"`    //企业Id
	NavAudio string `json:"navAudio"` //导航语音,可选
	GetKey   bool   `json:"gegKey"`   //按键支持,可选，默认支持
	Acd      string `json:"acd"`      //分配模式
}

//企业号码绑定
type EnterpriseNumber struct {
	Number  string `json:"number"`  //接听号码/外显号码
	AppId   string `json:"appId"`   //企业Id
	Display bool   `json:"display"` //用于外显，还是呼入
}

//呼入配置
type CallInConfig struct {
	Number       string `json:"number"`       //中继号
	AppId        string `json:"appId"`        //企业Id
	TransferType string `json:"transferType"` //转接类型 service:坐席，queue:技能组,ivr:ivr
	Info         string `json:"info"`         //转接内容
}

//坐席注册入参
type SignParam struct {
	ServiceId string `json:"serviceId"` //坐席Id
	Number    string `json:"number"`    //接听号码
	SkillId   string `json:"skillId"`   //队列名称/队列Id
	AppId     string `json:"appId"`     //企业Id
	Display   string `json:"display"`   //外显号码
}

//坐席状态
type ServiceStatus struct {
	AppId     string `json:"appId"`
	ServiceId string `json:"serviceId"` //坐席Id
	Status    string `json:"status"`    //free / busy
}

//队列入参
type QueParam struct {
	Id       int    `json:"id"`       //队列Id
	AppId    string `json:"appId"`    //企业Id
	Name     string `json:"name"`     //队列名称--企业内唯一
	Key      string `json:"key"`      //绑定按键/关键字，目前是数字0-9或*、#
	Media    string `json:"media"`    //入队等待音 可选
	QueLen   int    `json:"queLen"`   //队列长度，可选
	WaitTime int    `json:"waitTime"` //队列等待时长 可选
	Distrb   string `json:"distrb"`   //队列分发模式 枚举,字符串
}

type SipSignParam struct {
	ServiceId string `json:"serviceId"` //坐席Id
	SipUser   string `json:"number"`    //坐席账号
	SipServ   string `json:"sipServer"` //sip注册服务器
	QueName   string `json:"skillQue"`  //队列名称/队列Id
	CompanyId string `json:"companyId"` //企业Id
	Display   string `json:"display"`   //外显号码
}

type SipCallBack struct {
	Service  string `json:"service"`  //坐席注册号
	Number   string `json:"number"`   //被叫号码
	Display  string `json:"display"`  //外显号码
	ReqId    string `json:"reqId"`    //请求Id
	AppId    string `json:"appId"`    //devId
	CallBack string `json:"callBack"` //回调URL
}

type GuessCallParam struct {
	CompanyId string `json:"companyId"` //企业Id
	TaskId    string `json:"taskId"`    //外呼Id 企业内唯一
	Speed     int    `json:"speed"`     //外呼频率 ,每秒呼叫个数

}

type TaskNumberParam struct {
	AppId   string   `json:"appId"`   //企业Id
	TaskId  string   `json:"taskId"`  //外呼Id 企业内唯一
	Numbers []string `json:"numbers"` //外呼号码,10万以内，超过10万会被丢弃
}

type SipAcctParam struct {
	AppId      string    `json:"appId"`   //企业Id
	SipAcct    string    `json:"account"` //账号名称 16位
	Passwd     string    `json:"passwd"`  //账号密码 建议6位数字
	Active     bool      `json:"-"`
	CreateTime time.Time `json:"-"`
}

type ActiveTime struct {
	From string `json:"from"`
	End  string `json:"end"`
}

type IvrParam struct {
	IvrId      string       `json:"ivrId"`
	AppId      string       `json:"appId"`
	Name       string       `json:"name"`
	Number     string       `json:"number"`
	Day        string       `json:"day"`
	ActiveTime []ActiveTime `json:"activeTime"` //生效时间
	Active     bool         `json:"active"`
}

type HangParam struct {
	AppId  string `json:"appId"`
	CallId string `json:"callId"`
}
